﻿using Exiled.API.Features;
using Exiled.Events.EventArgs.Player;
using Exiled.Events.Features;
using System;
using System.Collections.Generic;
using static Exiled.Events.Events;

namespace RgCore
{
    public struct DamageInfo
    {
        public DamageInfo(string userid, float maxhealth)
        {
            Player = userid;
            MaxHealth = maxhealth;
            Damages = new Dictionary<string, float>();
        }

        public readonly string Player;
        public readonly float MaxHealth;
        public Dictionary<string, float> Damages;

        public void DealDamage(string attacker_userid, float amount)
        {
            if (!Damages.ContainsKey(attacker_userid))
                Damages.Add(attacker_userid, 0f);
            Damages[attacker_userid] += amount;
        }
    }

    public static class DamageRecorder
    {
        public static List<DamageInfo> Damages = new List<DamageInfo>();

        public static event CustomEventHandler<HurtingEventArgs> OnHurting;

        public static event Action<HurtingEventArgs> DamageResult;

        public static void Start()
        {
            Exiled.Events.Handlers.Player.Hurting += Player_Hurting;
            Exiled.Events.Handlers.Player.Spawned += Player_Spawned;
            Exiled.Events.Handlers.Player.Died += Player_Died;
        }

        private static void Player_Died(DiedEventArgs ev)
        {
            //if (Damages.ContainsKey(ev.Player.UserId))
            //{
            //    var list = new Dictionary<string, float>(Damage[ev.Player.UserId]);
            //    List<string> text = new List<string>() { "你生前受到了以下玩家的伤害：" };
            //    foreach (var item in list)
            //    {
            //        Player attacker = Player.Get(item.Key);
            //    PlayerRoleBase
            //        if (attacker != null)
            //        {
            //            damage.Add(attacker, item.Value);
            //            int percent = (int)(Math.Round(item.Value / ev.TargetOldRole., 2) * 100);
            //            if (attacker != null)
            //            {
            //                text.Add($"{attacker.Nickname} [<color=red>{percent}%</color>]");
            //            }
            //        }
            //    }
            //    ev.Player.SendRawMsg("damage", text.ToArray(), 7, ScreenLocation.CenterBottom, false);
            //    Damage[ev.Player.UserId].Clear();
            //}
        }

        private static void Player_Spawned(SpawnedEventArgs ev)
        {
        }

        private static void Player_Hurting(HurtingEventArgs ev)
        {
            if (ev.Player.Role.Team == PlayerRoles.Team.SCPs)
            {
                ev.Amount *= RgCore.instance.Config.ScpDealtDamageMultiplier;
            }
            if (OnHurting != null)
            {
                foreach (CustomEventHandler<HurtingEventArgs> a in OnHurting.GetInvocationList())
                {
                    try
                    {
                        a.Invoke(ev);
                        if (!ev.IsAllowed)
                            break;
                    }
                    catch (NullReferenceException)
                    {
                        continue;
                    }
                }
                if (!ev.IsAllowed || ev.Amount == 0)
                {
                    return;
                }
                DamageResult?.Invoke(ev);
            }
        }
    }
}